﻿using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace Tools
{
    public class ScriptsFind
    {


        /// <summary>
        /// 从新打开项目
        /// </summary>
        [MenuItem("EzFreamWork/ReopenProject &r")]
        static void DoReopenProject()
        {
            EditorApplication.OpenProject(Application.dataPath.Replace("Assets", string.Empty));
        }

        [MenuItem("Assets/查找脚本挂在哪些物体上")]
        static void GetReference()
        {
            string target = "";
            if (Selection.activeObject != null)
                target = AssetDatabase.GetAssetPath(Selection.activeObject);
            if (string.IsNullOrEmpty(target))
                return;
            string[] files = Directory.GetFiles(Application.dataPath, "*.prefab", SearchOption.AllDirectories);
            string[] scene = Directory.GetFiles(Application.dataPath, "*.unity", SearchOption.AllDirectories);

            List<Object> filelst = new List<Object>();
            for (int i = 0; i < files.Length; i++)
            {
                string[] source = AssetDatabase.GetDependencies(new string[] { files[i].Replace(Application.dataPath, "Assets") });
                for (int j = 0; j < source.Length; j++)
                {
                    if (source[j] == target)
                        filelst.Add(AssetDatabase.LoadMainAssetAtPath(files[i].Replace(Application.dataPath, "Assets")));
                }
            }
            for (int i = 0; i < scene.Length; i++)
            {
                string[] source = AssetDatabase.GetDependencies(new string[] { scene[i].Replace(Application.dataPath, "Assets") });
                for (int j = 0; j < source.Length; j++)
                {
                    if (source[j] == target)
                        filelst.Add(AssetDatabase.LoadMainAssetAtPath(scene[i].Replace(Application.dataPath, "Assets")));
                }
            }
            Selection.objects = filelst.ToArray();
        }
    }
}
